import { Store } from 'iflux2';
import {fromJS} from 'immutable'
import SectionActor from '../actor/section-actor.js';
import {SectionTypes} from '../constants/actionTypes.js'
import DomainActor, {DomainTypes} from 'actor/domain/index';
import webapi from './webapi.js';
import { message } from 'antd'
export default class AppStore extends Store {
    bindActor () {
        return [
            new SectionActor,
            new DomainActor,
        ]
    }
    /**
     * 构造函数
     * @param {*} props 
     */
    constructor(props){
        super(props);
    }

    fetchDomain = () => {
        webapi.fetchDomain().then(result => {
            this.dispatch(DomainTypes.RECEIVE_DOMAIN_DATA,fromJS(result[0]));
        })
    }
    /**
     * 查询所有文章栏目
     */
    fetchArticleSections = ()=>{

        return webapi.fetchArticleSections().then(result=>{
            this.dispatch(SectionTypes.FETCH_SECTIONS,fromJS(result));
            return result;
        }).catch((error)=>{
            message.error(error.message,5);
            return Promise.reject(error);
        })
        
    }
    /**
     * 新增文章列表
     * @param section 栏目
     */
    addArticleSection = (section) =>{
        return webapi.submitArticleSection(section)
            .then((result)=>{
                this.dispatch(SectionTypes.ADD_SECTION,fromJS(result));
                message.success('新增栏目成功！',5)
                return result;
            })
            .catch(error=>{
                message.error(error.message,5);
                return Promise.reject(error);
            })
    }
    /**
     * 修改栏目
     * @param section 栏目
     */
    updateArticleSection = (section) => {

        return webapi.submitArticleSection(section)
            .then(result=>{
                this.dispatch(SectionTypes.UPDATE_SECTION,fromJS(result))
                message.success('修改栏目成功！',5)
                return result;
            })
            .catch(error=>{
                message.error(error.message,5);
                return Promise.reject(error);
            })
    }
    /**
     * 删除文章栏目
     * @param id 文章栏目编号
     */
    deleteArticleSection = (id) => {
        webapi.deleteArticleSection(id)
            .then(result=>{
                this.dispatch(SectionTypes.DELETE_SECTION,id);
                message.success('删除栏目成功！',5)
            })
            .catch(error=>{
                message.error(error.message,5);
                return Promise.reject(error);
            })
    }

    /**
     * 新增文章列表
     * @param article 文章
     */
    addArticle = (article) =>{
        return webapi.submitArticle(article)
            .then((result)=>{
                message.success('新增文章成功！',5)
                //this.dispatch(ArticleTypes.ADD_ARTICLE,fromJS(result))
                return result;
            })
            .catch(error=>{
                return Promise.reject(error);
            })
        }


}